package com.hankol.material.api;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baselibrary.utils.ScreenUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 自动播放的工具
 */
public class MaterialAutoPlayTool {
    public MaterialAutoPlayItem mHolder;
    private int visiblePercent = 60;
    public static int MODE_PLAY_FIRST = 0;
    public static int MODE_PLAY_CENTER = 1;
    private int mode = MODE_PLAY_FIRST;
    public MaterialAutoPlayItem.HidedVideoListener mHidedVideoListener;

    public MaterialAutoPlayTool() {

    }

    public MaterialAutoPlayTool(int visiblePercent) {
        this.visiblePercent = visiblePercent;
    }

    public MaterialAutoPlayTool(int visiblePercent, int mode) {
        this.visiblePercent = visiblePercent;
        this.mode = mode;
        mHidedVideoListener = new MaterialAutoPlayItem.HidedVideoListener() {
            @Override
            public void hide() {
                if (mHolder != null)
                    mHolder.deactivate();
            }

            @Override
            public void show() {
                if (mHolder != null)
                    mHolder.setActive();
            }
        };
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    /**
     * 当滑动停止的时候，开始视频播放
     *
     * @param recyclerView
     * @return
     */
    public int onActiveWhenNoScrolling(RecyclerView recyclerView, Context context) {
        LinearLayoutManager layoutManager = null;
        if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
            layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        }
        if (layoutManager != null) {
            int firstItemPosition = layoutManager.findFirstVisibleItemPosition();
            int lastItemPosition = layoutManager.findLastVisibleItemPosition();
            LinkedHashMap<Integer, MaterialAutoPlayItem> items = new LinkedHashMap();
            while (firstItemPosition <= lastItemPosition) {
                RecyclerView.ViewHolder holder = recyclerView.findViewHolderForLayoutPosition(firstItemPosition);

                if (holder instanceof MaterialAutoPlayItem) {
//                    View view = (((MaterialAutoPlayItem) holder)).getAutoplayView();
//                    if (view != null && getVisible(view, visiblePercent)) {
//                        if (mode == MODE_PLAY_FIRST) {//优先播放第一个的情况
//                            ((MaterialAutoPlayItem) holder).setActive();
//                            mHolder = ((MaterialAutoPlayItem) holder);
//                            return firstItemPosition;
//                        }
//                        items.put(firstItemPosition, ((MaterialAutoPlayItem) holder));
//                    }
                }
                firstItemPosition++;
            }
            //下面的逻辑是播放靠中间的视频
            int d = Integer.MAX_VALUE;
            MaterialAutoPlayItem findHolder = null;
            int position = -1;
            //找出距离中间最近的一个
            for (Map.Entry<Integer, MaterialAutoPlayItem> entry : items.entrySet()) {
//                int d2 = getDistanceFromCenter(entry.getValue().getAutoplayView(), context);
//                if (d2 < d) {
//                    findHolder = entry.getValue();
//                    d = d2;
//                    position = entry.getKey();
//                }
            }
            if (mHolder != findHolder) {
//                if (mHolder != null && mHolder.getAutoplayView() != null) {
//                    mHolder.deactivate();
//                }
//                mHolder = findHolder;
            }
            if (mHolder != null) {
//                if (mHolder.getAutoplayView() != null) {
//                    mHolder.setActive();
//                    return position;
//                }
            }
        }
        return -1;
    }

    //当视频画出屏幕时停止播放
    public void onScrolledAndDeactivate(RecyclerView recyclerView) {
//        if (mHolder != null && mHolder.getAutoplayView() != null && !getVisible(mHolder.getAutoplayView(), visiblePercent)) {
//            mHolder.deactivate();
//        }
    }

    /**
     * 用于停止滑出去的视频
     */
    public void onScrolledAndDeactivate() {
//        if (mHolder != null && mHolder.getAutoplayView() != null && !getVisible(mHolder.getAutoplayView(), visiblePercent)) {
//            mHolder.deactivate();
//        }
    }

    public void setVisiblePercent(int visiblePercent) {
        this.visiblePercent = visiblePercent;
    }

    private int getVisiblePercent(View v) {
        Rect r = new Rect();
        boolean visible = v.getLocalVisibleRect(r);
        if (visible && v.getMeasuredHeight() > 0) {
            int percent = 100 * r.height() / v.getMeasuredHeight();
            return percent;
        }
        return -1;
    }

    private boolean getVisible(View v, int value) {
        Rect r = new Rect();
        boolean visible = v.getLocalVisibleRect(r);
        if (visible && v.getVisibility() == View.VISIBLE) {
            if (getVisiblePercent(v) >= value) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }


    private int getDistanceFromCenter(View view, Context context) {
        return ScreenUtils.getScreenHeight();
    }

}